/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bdd;

import Model.*;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Sik
 */
public class ControllerBDD
{
    private static final ControllerBDD INSTANCE = new ControllerBDD();
    
    private ControllerBDD()
    {
    }
    
    public List<Adherent> importAdherent()
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        Query q = session.getNamedQuery("Adherent.findAll");
        t.commit();
        return q.list();
    }
    
    public List<Adherent> importSaison()
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        Query q = session.getNamedQuery("Saison.findAll");
        t.commit();
        return q.list();
    }
    
    public void add(Adherent a)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(a);
        t.commit();
    }

    public void add(Utilisateur u)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(u);
        t.commit();
    }
    
    public void add(Droits d)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(d);
        t.commit();
    }
        
    public void add(Saison s)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(s);
        t.commit();
    }
    
    public void add(Model.Transaction tr)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(tr);
        t.commit();
    }
        
    public void add(Deplacement d)
    {
        Session session = HibernateUtil.getSession();
        Transaction t = session.beginTransaction();
        session.save(d);
        t.commit();
    }
    
    public void update(Adherent a)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.update(a);
        tx.commit();
    }
    
    public void update(Utilisateur u)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.update(u);
        tx.commit();
    }
        
    public void update(Model.Transaction tr)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.update(tr);
        tx.commit();
    }
    
    public void update(Deplacement d)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.update(d);
        tx.commit();
    }
    
    public void remove(Utilisateur u)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.delete(u);
        tx.commit();
    }
    
    public void remove(Adherent a)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.delete(a);
        tx.commit();
    }

    public void remove(Deplacement d)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.delete(d);
        tx.commit();
    }
    
    public void remove(Model.Transaction tr)
    {
        Session s = HibernateUtil.getSession();
        Transaction tx = s.beginTransaction();
        s.delete(tr);
        tx.commit();
    }
    
    public static ControllerBDD getINSTANCE()
    {
        return INSTANCE;
    }
}
